﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using CommontLibrary;
public partial class admin_insertTruong : System.Web.UI.UserControl
{

#region "Member variables"
    int intLanguageID;
    private int intUserID = 0;
    private int intModuleID = 0;
    bool boolRead = false;
    bool boolWrite = false;
    bool boolUpdate = false;
    bool boolDelete = false;
    DATruong objTruong;
    DANguoidung objNguoiDung;
#endregion

#region "Private methods"
    // Hien thi Truong tren datagrid
    private void loadTruong()
    {
        DataSet dsTruong = objTruong.LayDanhSachTruong();
        BindData.bindGridView(this.ddlTruong, dsTruong);
    }
    //check validation
    private bool checkValidation()
    {
        if (this.txtTruongName.Text.Trim().Equals(""))
        {
            this.ctvTruongName.IsValid = false;
            return false;
        }
        else
        {
            return true;
        }
    }
    //Reset text cua from    
    private void resetFrom()
    {
        this.txtTruongName.Text = "";
    }
    // Sua Truong
    private void UpdateGroup(object source, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
    {
        int intTruongid = Convert.ToInt32(ddlTruong.DataKeys[e.RowIndex].Value);
        string strTruongName = "";
        bool boolTruongStatus = false;
        foreach (Control ctrl in ddlTruong.Rows[e.RowIndex].Cells[2].Controls)
        {
            if (ctrl.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
            {
                strTruongName = ((TextBox)ctrl).Text;
                break;
            }
        }
        if (strTruongName.Equals(""))
        {
            System.Web.UI.WebControls.CustomValidator rfvTemp = null;
            foreach (Control ctrl in ddlTruong.Rows[e.RowIndex].Cells[2].Controls)
            {
                if (ctrl.GetType().ToString() == "System.Web.UI.WebControls.CustomValidator")
                {
                    rfvTemp = ((CustomValidator)ctrl);
                    break;
                }
            }
            rfvTemp.ErrorMessage = "Bạn phải nhập tên nhóm liên kết";
            rfvTemp.IsValid = false;
        }
        else
        {
            int intChẹck = objTruong.updateTruong(intTruongid, strTruongName);
            if (intChẹck > 0)
            {
                this.lblMess.Text = "Sửa thành công bản ghi";
                ddlTruong.EditIndex = -1;
                loadTruong();
            }
            else if (intChẹck == 0)
            {
                this.lblMess.Text = "Bản ghi này đã có trong dữ liệu!";
            }
            else
            {
                this.lblMess.Text = "Lỗi trong quá trình sửa bản ghi!";
            }
        }
    }
    // Tao moi Truong
    private void CreateGroup()
    {
        if (Page.IsValid)
        {
            if (checkValidation())
            {
                string strTruongName = this.txtTruongName.Text.Trim();
                int intChẹck = objTruong.insertTruong(strTruongName, intLanguageID);
                if (intChẹck > 0)
                {
                    this.lblMess.Text = "Thêm bản ghi thành công";
                    resetFrom();
                    loadTruong();
                }
                else if (intChẹck == 0)
                {
                    this.lblMess.Text = "Bản ghi này đã có trong dữ liệu!";
                }
                else
                {
                    this.lblMess.Text = "Lỗi trong quá trình thêm mới bản ghi!";
                }
            }
        }
    }
    // Xoa Truong
    private void DeleteTruong(object source, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
    {
        int intTruongid = Convert.ToInt32(ddlTruong.DataKeys[e.RowIndex].Value);
        int intChẹck = objTruong.removeTruong(intTruongid);
        if (intChẹck > 0)
        {
            this.lblMess.Text = "Xóa thành công bản ghi";
            ddlTruong.EditIndex = -1;
            loadTruong();
        }
        else
        {
            this.lblMess.Text = "Lỗi trong quá trình xóa bản ghi!";            
        }
    }

#endregion
#region "Parametter variables"
    // Ham lay Bind du lieu
    public SqlConnection ConnectionView
    {
        get
        {
            object obj2 = this.ViewState["ConnectionView"];
            if (obj2 != null)
            {
                return (SqlConnection)obj2;
            }
            return null;
        }
        set
        {
            this.ViewState["ConnectionView"] = value;
        }
    }
    #endregion     
#region "Events handle"
    //Check quyen han su dung
    private void CheckUserModule(int UserID, int ModuleID)
    {
        //objNguoiDung.CheckUserModule(UserID, ModuleID, ref boolRead, ref boolWrite
        //    , ref boolUpdate, ref boolDelete);
        //if (!boolRead)
        //{
        //    this.btnInsert.Visible = false;
        //    this.ddlTruong.Visible = false;
        //}
        //if (!boolWrite)
        //{
        //    this.btnInsert.Visible = false;
        //}
        //if (!boolUpdate)
        //{
        //    this.ddlTruong.Columns[3].Visible = false;
        //    this.ddlTruong.Columns[5].Visible = false;
        //    this.btnRemove.Visible = false;
        //}
        //if (!boolDelete)
        //{
        //    this.ddlTruong.Columns[4].Visible = false;
        //    this.ddlTruong.Visible = false;
        //    this.ddlTruong.Visible = true;
        //}
        //else
        //{
        //    this.ddlTruong.Visible = true;
        //}
    }
    private void loadDataAccess()
    {
        SqlConnection sqlConnec = ConnectData.getConnection();
        objTruong = new DATruong(sqlConnec);
        objNguoiDung = new DANguoidung(sqlConnec);
    }
    //Main load
    protected void Page_Load(object sender, EventArgs e)
    {
        // if (ConnectionView != null)
        {
            intUserID = Convert.ToInt32(Session["UserID"].ToString().Trim());
            loadDataAccess();
            if (!Page.IsPostBack)
            {
                CheckUserModule(intUserID, intModuleID);
                loadTruong();
            }
           // this.ConnectionView = null;
        }
    }
    //Su kien Tao moi Truong
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        this.lblMess.Text = "";
        CreateGroup();
    }
    //Su kien rest form Truong
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        this.lblMess.Text = "";
        resetFrom();
    }
    //Su kien bin from Truong
    protected void ddlTruong_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes["onmouseover"] = "javascript:setMouseOverColor(this);";
                e.Row.Attributes["onmouseout"] = "javascript:setMouseOutColor(this);";
                
                // Modify
                ImageButton lbtDeleteTemp = ((ImageButton)e.Row.FindControl("lbtDelete"));
                if (lbtDeleteTemp != null)
                {
                    string strDelete = "Bạn chắc chắn muốn xóa OK , Bạn hủy lệnh CANCEL ";
                    lbtDeleteTemp.Attributes.Add("Onclick", "javascript:if(confirm('" + strDelete + "')) {return true} else {return false};");
                }
            }
        }

    }
    //Su kien cancel cua edit Truong
    protected void ddlTruong_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.lblMess.Text = "";
        ddlTruong.EditIndex = -1;
        loadTruong();
    }
    //Su kien xoa Truong
    protected void ddlTruong_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        this.lblMess.Text = "";
        DeleteTruong(sender, e);
    }
    //Su kien edit form Truong
    protected void ddlTruong_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.lblMess.Text = "";
        ddlTruong.EditIndex = e.NewEditIndex;
        loadTruong();
    }
    //Su kien update Truong
    protected void ddlTruong_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        this.lblMess.Text = "";
        UpdateGroup(sender, e);
    }
    protected void ddlTruong_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.ddlTruong.EditIndex = -1;
        this.ddlTruong.PageIndex = e.NewPageIndex;
        this.loadTruong();
    }
#endregion

}
